﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace AutoRequest
{
    public static class StringPlus
    {
        public static string DelLastChar(this string str, string strchar)
        {
            return str.Substring(0, str.LastIndexOf(strchar));
        }
        public static IEnumerable<string> SplitString(this string input, char splstr, bool removeempty)
        {
            if (!string.IsNullOrEmpty(input))
            {
                return
                    from c in input.Split(new char[]
					{
						splstr
					})
                    where !removeempty || !string.IsNullOrEmpty(c)
                    select c;
            }
            return null;
        }
        public static string ToSBC(this string input)
        {
            char[] array = input.ToCharArray();
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] == ' ')
                {
                    array[i] = '\u3000';
                }
                else
                {
                    if (array[i] < '\u007f')
                    {
                        array[i] += 'ﻠ';
                    }
                }
            }
            return new string(array);
        }
        public static string ToDBC(this string input)
        {
            char[] array = input.ToCharArray();
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] == '\u3000')
                {
                    array[i] = ' ';
                }
                else
                {
                    if (array[i] > '＀' && array[i] < '｟')
                    {
                        array[i] -= 'ﻠ';
                    }
                }
            }
            return new string(array);
        }
        public static string NoHTMLLabel(this string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return str;
            }
            Regex regex = new Regex("</?.*?>", RegexOptions.IgnoreCase);
            string text = regex.Replace(str, "");
            return text.Replace("\n", "").Replace("\r", "").Trim();
        }
        public static string EmptyHTMLLabel(this string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return str;
            }
            Regex regex = new Regex("<(\\w+)\\s+.*?>.*?</\\1>", RegexOptions.IgnoreCase);
            string text = regex.Replace(str, "");
            return text.Replace("\n", "").Replace("\r", "").Trim();
        }
        public static string NoHTMLLabel(this string str, string labelName)
        {
            if (string.IsNullOrEmpty(str))
            {
                return str;
            }
            Regex regex = new Regex("</?" + labelName + ".*?>", RegexOptions.IgnoreCase);
            return regex.Replace(str, "");
        }
        public static string NoHTMLLabel(this string str, int charLength)
        {
            string text = str.NoHTMLLabel();
            if (text.Length <= charLength)
            {
                return text;
            }
            return text.Substring(0, charLength);
        }
        public static string NoSpace(this string str)
        {
            return str.Replace(" ", "");
        }
        public static string PieceOfString(this string source, int startIndex, int length)
        {
            if (source.Length < startIndex)
            {
                return string.Empty;
            }
            if (source.Length < startIndex + length)
            {
                return source.Substring(startIndex);
            }
            return source.Substring(startIndex, length);
        }
        public static bool Contains(this string str, string regexp, RegexOptions option)
        {
            Regex regex = new Regex(regexp, option);
            return regex.IsMatch(str);
        }
    }
}
